package BankManageSystem;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Bank {
    List<Account> accounts = new ArrayList<>();

    Scanner scan = new Scanner(System.in);
    Operation operation = new Operation();

    public Bank() {
    }

//    public Bank(Account account) {
//        this.account = account;
//    }

    public void start(){
        isFlag:while (true) {
            System.out.println("===欢迎进入银行系统===");
            System.out.println("请选择你要进行的操作");
            System.out.println("1.用户登录");
            System.out.println("2.用户注册");
            System.out.println("3.退出");
            System.out.println("4.管理员系统");
            int command = scan.nextInt();
            switch (command){
                case 1:
                    //.用户登录
                    login();
                    break;
                case 2:
                    //用户注册
                    createAccount();
                    break;
                case 3:
                    break isFlag;
                case 4:
                    int order = 1254088;
                    System.out.println("请输入管理员密码：");
                    int okOrder = scan.nextInt();
                    if(okOrder == order){
                        try {
                            writeFile();
                            readFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        break isFlag;
                    }
                        break isFlag;
                default:
                    System.out.println("输入有误，请重新输入");

            }
        }

    }
    public void createAccount(){
        Account acc = new Account();

        System.out.println("请输入你的名字:");
        String name = scan.next();
        acc.setName(name);
        System.out.println("请输入你的密码:");
        while (true) {
            int password = scan.nextInt();
            System.out.println("请再次输入密码进行确认：");
            int okPassword = scan.nextInt();
            if(password == okPassword){
                acc.setPassword(password);
                break;
            }else{
                System.out.println("输入密码不一致，请重新输入");
            }
        }
        int accountId = (int)(Math.random()*(900000))+100000;
        System.out.println("你的卡号为" + accountId);
        acc.setAccountId(accountId);

        accounts.add(acc);
//        for(Account a: accounts){
//            System.out.println(a);
//        }
        System.out.println("注册成功");
    }
    public void login(){
        System.out.println("===登录系统===");
        System.out.println("请输入卡号");
        int Id = scan.nextInt();
        Account acc = findAccount(Id);

        if(acc == null){
            System.out.println("该卡号不存在");
        }else{
            System.out.println("请输入密码");
            int passWord = scan.nextInt();
            if(acc.getPassword() == passWord){
                System.out.println("登录成功");
                operation.show();
                return;
            }else{
                System.out.println("密码错误");
            }
        }
//        for(Account a: accounts){
//            System.out.println(a);
//        }
    }
    public Account findAccount(int Id){
        for(int i = 0;i<accounts.size();i++){
            Account acc = accounts.get(i);
            if(acc.getAccountId() == Id){
                return acc;
            }
        }
        return null;
    }
    public void writeFile() throws IOException {
        String fileName = "user.txt";

        BufferedWriter fw= null;
        try {
            fw = new BufferedWriter(new FileWriter(fileName,true));
            for(Account account : accounts){
                String line = account.getName()+","+account.getAccountId();
                fw.write(line);
                fw.newLine();
            }
        } catch (IOException e) {
            System.out.println("用户储存失败");
        }finally {
            fw.close();
        }
    }
    public void readFile() throws IOException {
        String fileName = "user.txt";
        BufferedReader fr=null;
        try {
            fr = new BufferedReader(new FileReader(fileName));
            String line;
            System.out.println("银行用户如下：");
            while((line = fr.readLine())!=null){
                System.out.println(line);
            }
        }catch (IOException e){
            System.out.println("读取文件发生错误");
        }finally {
            fr.close();
        }
    }

}
